/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov;

import java.util.Arrays;

/**
 * Represents the state of an agent, i.e. its current perception of the environment.
 *
 * @author Matthijs Snel
 * @version 0.2, 16/05/2009
 */
public class VectorState extends State<double[]> implements Cloneable {
	
	private static final long serialVersionUID = 1792432571268320644L;
	
	public VectorState() {
		super();
	}
	
	public VectorState( double[] data ) {
		this(data, false);
	}
	
	public VectorState( double[] data, boolean terminal ) {
		super(data, terminal);
	}
	
	/**
	 * Append another vector to the end of the current state vector. If the state vector is 
	 * currently null, it will be set equal to the vector passed as argument, i.e. any 
	 * modifications in the vector passed as argument will be reflected in the State.
	 * 
	 * @param vector the vector to append
	 */
	public void append( double[] vector) {
		if ( vector == null || vector.length == 0 )
			return;
		if ( data == null )
			data = vector;
		else {
			int len = data.length;
			data = Arrays.copyOf(data, len + vector.length);
			System.arraycopy(vector, 0, data, len, vector.length);
		}
	}
	
	/**
	 * Compares this object against the specified object. The result is true if and only if the
	 * argument is not null and is a {@code VectorState} of which the terminal value matches the terminal 
	 * value of this object, the state vector has the same length as the vector of this
	 * object, and all values in the vectors are equal according to the method specified in
	 * {@link Double#equals(Object)}.
	 * 
	 * Note that it usually doesn't make much sense to compare double values for equality, so unless
	 * the double values in the vector actually represent integers, or only a discrete set of
	 * values (e.g. either 0, 0.25, 0.5, 0.75 or 1), this method will usually return false.
	 * 
	 * @param the object to compare with
	 * @return {@code true} if the objects are the same; {@code false} otherwise.
	 */
	@Override
	public boolean equals( Object o ) {
		if ( o == null || !(o instanceof VectorState) )
			return false;
		VectorState s = (VectorState) o;
		if (terminal == s.terminal && data.length == s.data.length) {
			return Arrays.equals(data, s.data);
		} else
			return false;
	}
	
	/**
	 * Returns the hash code for this state, according to the methods specified in {@link List}
	 * and {@link Double}.
	 */
	@Override
	public int hashCode() {
		int hashCode = Arrays.hashCode(data);
		hashCode = 31*hashCode + (terminal ? 1 : 0);
		return hashCode;
	}
	
	@Override
	public String toString() {
		String s = "";
		for ( int i = 0; i < data.length; i++ ) {
			s += data[i] + " ";
		}
		return s;
	}
}

